import { login, getInfo } from '@/api/login'
import { getList, edit, updateApi, add, del, query, getPageList } from '@/api/table'
import Cookies from 'js-cookie'
import router from '../../router'
// import { Message } from 'element-ui'
const user = {
  state: {
    userinfo: { // 获取的用户的列表
    },
    p: {},
    currPage: null,
    updateinfo: {}, // 个人详细信息
    show: { // 显示不同的标签
      showUpdate: false,
      centerDialogVisible: false,
      formInline: {
        user: '',
        condition: ''
      },
      title: false
    },
    admininfo: {
    }
  },

  mutations: {
    LOGIN: (state, info) => {
      // this.getInfo()
      state.admininfo = info
      Cookies.set('adminName', state.admininfo.username)
    },
    GETINFO: (state) => { // 根据id获取用户的详细信息
      console.log(state)
      // state.userinfo.image = userinfo.image
    },
    // 获取用户列表
    GETUSERLIST: (state, info) => {
      state.userinfo = ''
      state.userinfo = info
    },
    EDITUSER: (state, data, index) => {
      // console.log(data)
      state.updateinfo = data
      state.show.title = true
      state.show.showUpdate = true
      // state.userinfo.list[index].status = state.userinfo.list[index].status
    },
    ADDUSER: (state, info) => {
      state.show.centerDialogVisible = false
      // console.log(info)
      // console.log(state.userinfo.list)
    },
    DEL: (state, index) => {
      state.userinfo.list.splice(index, 1)
    },
    DIALOGUPDATE: (state) => {
      state.show.centerDialogVisible = false
      state.show.showUpdate = false
      state.userinfo = ''
    },
    ADD: (state) => {
      state.show.centerDialogVisible = true
      state.show.title = false
    },
    CLEAR: (state) => {
      state.updateinfo = {}
    },
    FED_LOGOUT: (state, i) => {
      Cookies.remove('adminName')
    },
    QUERY: (state, info) => {
      state.userinfo = {}
      state.userinfo = info
    },
    CHANGEPAGE: (state, info, page) => {
      state.userinfo = {}
      state.userinfo = info
      state.currPage = page
      state.p = {
        curr: state.currPage
      }
    }
  },

  actions: {
    changePage({ commit, state }, page) { // 点击页数切换页
      return new Promise((resolve, reject) => {
        var p = {
          curr: page
        }
        getPageList(p).then((res) => {
          commit('CHANGEPAGE', res.content)
          resolve(res)
        }).then(error => {
          reject(error)
        })
      })
    },
    ReturnIndex({ commit, state }) {
      router.push('/')
    },
    QueryUser({ commit, state }) { // 条件查询
      return new Promise((resolve, reject) => {
        var p = {
          name: state.show.formInline.user,
          status: state.show.formInline.condition,
          curr: state.userinfo.pageNumber
        }
        query(p).then((res) => {
          console.log(res)
          commit('QUERY', res.content)
          p = {}
          resolve(res)
        }).then(error => {
          reject(error)
        })
      })
    },
    clear({ commit }) {
      console.log(0)
      commit('CLEAR')
    },
    Add({ commit }) {
      commit('ADD')
    },
    dialogUpdate({ commit, state }) {
      return new Promise((resolve, reject) => {
        var info = {
          'user.id': state.updateinfo.id,
          'user.name': state.updateinfo.name,
          'user.username': state.updateinfo.username,
          'user.password': state.updateinfo.password,
          'user.department': state.updateinfo.department,
          'user.phone': state.updateinfo.phone,
          'user.image': state.updateinfo.image,
          'user.des': state.updateinfo.des,
          'user.status': state.updateinfo.status
        }
        updateApi(info).then(res => {
          commit('DIALOGUPDATE')
          resolve(res)
        }).then(error => {
          reject(error)
        })
      })
    },
    // 删除用户
    Del({ commit, state }, index) {
      var id = state.userinfo.list[index].id
      console.log(id)
      return new Promise((resolve, reject) => {
        del(id).then(res => {
          console.log(res)
          commit('DEL', index)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 添加用户
    addUser({ commit, state }) {
      // console.log(userInfo)
      return new Promise((resolve, reject) => {
        var info = {
          'user.id': state.updateinfo.id,
          'user.name': state.updateinfo.name,
          'user.username': state.updateinfo.username,
          'user.password': state.updateinfo.password,
          'user.department': state.updateinfo.department,
          'user.phone': state.updateinfo.phone,
          'user.image': state.updateinfo.image,
          'user.des': state.updateinfo.des,
          'user.status': state.updateinfo.status
        }
        add(info).then(res => {
          // console.log(res)
          // state.userinfo
          state.updateinfo = {}
          commit('ADDUSER', info)
          resolve(res)
        }).then(error => {
          reject(error)
        })
      })
    },
    // upNewInfo({ commit, state }) {
    //   // console.log(state.updateinfo)
    //   var info = {
    //     'user.id': state.updateinfo.id,
    //     'user.name': state.updateinfo.name,
    //     'user.username': state.updateinfo.username,
    //     'user.password': state.updateinfo.password,
    //     'user.department': state.updateinfo.department,getPageList
    //     'user.phone': state.updateinfo.phone,
    //     'user.image': state.updateinfo.image,
    //     'user.des': state.updateinfo.des,
    //     'user.status': state.updateinfo.status
    //   }
    //   return new Promise((resolve, reject) => {
    //     update(info).then(res => {
    //       resolve(res)
    //     }).then(error => {
    //       reject(error)
    //     })
    //   })
    // },
    // 获取更改用户的信息
    editUser({ commit, state }, index) {
      state.show.centerDialogVisible = true
      const userId = state.userinfo.list[index].id
      // console.log(index)
      return new Promise((resolve, reject) => {
        edit(userId).then(res => {
          var data = res.content
          commit('EDITUSER', data, index)
          resolve()
        }).catch(error => {
          console.log(error)
        })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        console.log(username)
        console.log(userInfo.password)
        login(username, userInfo.password).then(response => {
          debugger
          // const data = response
          console.log(response)
          console.log('`````data`````')
          commit('LOGIN', userInfo)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户列表
    GetUerList({ commit, state }) {
      return new Promise((resolve, reject) => {
        getList().then(res => {
          commit('GETUSERLIST', res.content)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          console.log(data)
          const data = response
          commit('GETINFO', data.content)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      // return new Promise((resolve, reject) => {
      //   logout(state.token).then(() => {
          // const data = response.data
      commit('FED_LOGOUT')
          // resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    }
    // // 前端 登出
    // FedLogOut({ commit }) {
    //   return new Promise(resolve => {
    //     commit('FED_LOGOUT', '')
    //     resolve()
    //   })
    // }
  }
}

export default user
